package bb.lanxing.common.engin.util;

import com.orm.SugarTransactionHelper;
import gov.nist.core.Separators;
import bb.lanxing.App;
import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.calc.processer.ProcessorFactory;
import bb.lanxing.calc.processer.i.IWorkoutProcessor;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.model.WorkoutDatabaseHelper;
import bb.lanxing.model.database.ITrackPoint;
import bb.lanxing.model.database.PacePoint;
import bb.lanxing.model.database.Segment;
import bb.lanxing.model.database.Trackpoint;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.Enums;
import java.util.LinkedList;
import java.util.List;

public class WorkoutSimulator {
    private static WorkoutSimulator instance;
    private static long workoutServerId;
    private Runnable finishCallback;
    private int pos;
    private IWorkoutProcessor processor;
    private Workout workout;
    private long timeDifference = -1;
    private Runnable simulatorRun = new Runnable() {
        @Override
        public void run() {
            WorkoutSimulator.this.cachePoint();
            while (!WorkoutSimulator.this.cachePoint.isEmpty()) {
                if (WorkoutSimulator.this.cachePoint.size() < 10) {
                    WorkoutSimulator.this.cachePoint();
                }
                ITrackPoint iTrackPoint = WorkoutSimulator.this.cachePoint.remove();
                if (iTrackPoint instanceof Trackpoint) {
                    Trackpoint trackpoint = (Trackpoint) iTrackPoint;
                    trackpoint.setSumCadence(1);
                    WorkoutSimulator.this.processor.process(new SourcePoint(trackpoint, WorkoutSimulator.this.workout));
                } else {
                    throw new IllegalArgumentException("Point type [" + iTrackPoint.getClass() + "] not support yet !");
                }
            }
            WorkoutSimulator.this.processor.release();
            Workout workout = WorkoutSimulator.this.processor.getWorkout();
            long unused = WorkoutSimulator.workoutServerId = 0L;
            App context = App.getContext();
            context.showLongMessage("save finished! " + workout.getId() + Separators.COLON + workout.getTitle());
            if (WorkoutSimulator.this.finishCallback != null) {
                WorkoutSimulator.this.finishCallback.run();
                WorkoutSimulator.this.finishCallback = null;
            }
        }
    };
    private LinkedList<ITrackPoint> cachePoint = new LinkedList<>();

    public void setFinishCallback(Runnable runnable) {
        this.finishCallback = runnable;
    }

    public static WorkoutSimulator getInstance() {
        if (instance == null) {
            instance = new WorkoutSimulator();
        }
        return instance;
    }

    public static void release() {
        if (instance != null) {
            instance = null;
        }
    }

    public static void setWorkoutServerId(long j) {
        workoutServerId = j;
    }

    public static boolean isSimulator() {
        return workoutServerId > 0;
    }

    private WorkoutSimulator() {
    }

    public boolean startSimulator() {
        Workout byServerId = Workout.getByServerId(workoutServerId);
        this.workout = byServerId;
        if (byServerId == null) {
            return false;
        }
        IWorkoutProcessor create = ProcessorFactory.create(ProcessorFactory.TYPE_SIMULATOR);//4
        this.processor = create;
        create.filterPoint(false);
        this.processor.init(RemoteSharedPreference.getInstance().getSportTypes());
        Workout workout = this.processor.getWorkout();
        workout.setSport(this.workout.getSport());
        workout.setUserId(this.workout.getUserId());
        workout.setTitle(this.workout.getTitle());
        workout.setHidden(this.workout.getHidden());
        workout.setDescription(this.workout.getDescription());
        workout.setLocSource(this.workout.getLocSource());
        workout.setCadenceSource(this.workout.getCadenceSource());
        workout.setHeartSource(this.workout.getHeartSource());
        workout.setPowerSource(this.workout.getPowerSource());
        workout.setTypeChangeCount(this.workout.getTypeChangeCount());
        workout.setUploadStatus(Enums.UploadStatus.NotUpload);
        WorkoutDatabaseHelper.save(workout);
        this.pos = 0;
        this.cachePoint.clear();
        new Thread(this.simulatorRun).start();
        return true;
    }

    public synchronized void cachePoint() {
        List<Trackpoint> queryTrackPoint = WorkoutDatabaseHelper.queryTrackPoint(this.workout.getId(), 5000L, this.pos);
        if (queryTrackPoint != null && !queryTrackPoint.isEmpty()) {
            this.cachePoint.addAll(queryTrackPoint);
            this.pos += queryTrackPoint.size();
        }
    }

    //TODO:sun
    private void deleteWorkout(final Workout workout) {
//        SugarTransactionHelper.doInTansaction(new SugarTransactionHelper.Callback() {
//            @Override
//            public void manipulateInTransaction() {
                Trackpoint.deleteByWorkout(workout.getId());
                Segment.deleteByWorkout(workout.getId());
                for (PacePoint pacePoint : WorkoutDatabaseHelper.queryPacePoint(workout.getUuid())) {
                    pacePoint.delete();
                }
                workout.delete();
            }
//        });
//    }
}
